package com.jianyun.jyzs.widget;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.jianyun.jyzs.R;


/**
 * Created by mlli on 2017/11/1.
 */

public class WaveViewBySinCos2 extends View {

    private Context mContext;
    /**
     * 振幅
     */
    private int A;
    private int B = 10;
    private int C = 20;
    /**
     * 偏距
     */
    private int K;

    /**
     * 波形的颜色
     */
    private int waveColor = 0xaa0096ec;
    /**
     * 波形的颜色
     */
    private int waveColor2 = 0xaa3fb9ff;
    /**
     * 波形的颜色
     */
    private int waveColor3 = 0x3F63c6ff;

    /**
     * 初相
     */
    private float φ;

    /**
     * 波形移动的速度
     */
    private float waveSpeed = 1f;

    /**
     * 角速度
     */
    private double ω;

    /**
     * 开始位置相差多少个周期
     */
    private double startPeriod;

    /**
     * 是否直接开启波形
     */
    private boolean waveStart;

    private Path path;
    private Path path2;
    private Path path3;

    private Paint paint;
    private Paint paint2;
    private Paint paint3;

    private static final int SIN = 0;
    private static final int COS = 1;

    private int waveType;

    private static final int TOP = 0;
    private static final int BOTTOM = 1;

    private int waveFillType;

    private ValueAnimator valueAnimator;


    public WaveViewBySinCos2(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        getAttr(attrs);
        K = A;
        initPaint();
        initAnimation();
    }

    private void getAttr(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.RadarWaveView);

        waveType = typedArray.getInt(R.styleable.RadarWaveView_waveType, SIN);
        waveFillType = typedArray.getInt(R.styleable.RadarWaveView_waveFillType, BOTTOM);
        A = typedArray.getDimensionPixelOffset(R.styleable.RadarWaveView_waveAmplitude, dp2px(30));
        waveColor = typedArray.getColor(R.styleable.RadarWaveView_waveColor, waveColor);
        waveSpeed = typedArray.getFloat(R.styleable.RadarWaveView_waveSpeed, waveSpeed);
        startPeriod = typedArray.getFloat(R.styleable.RadarWaveView_waveStartPeriod, 0);
        waveStart = typedArray.getBoolean(R.styleable.RadarWaveView_waveStart, false);

        typedArray.recycle();
    }

    private void initPaint() {
        path = new Path();
        path2 = new Path();
        path3 = new Path();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(waveColor);
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2.setStyle(Paint.Style.FILL_AND_STROKE);
        paint2.setColor(waveColor2);
        paint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint3.setStyle(Paint.Style.FILL_AND_STROKE);
        paint3.setColor(waveColor3);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        ω = 2 * Math.PI / getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        drawSin(canvas);
        drawCos(canvas);
//        draw3(canvas);

    }




    /**
     * 根据cos函数绘制波形
     *
     * @param canvas
     */
    private void drawCos(Canvas canvas) {
        fillCosBottom(canvas);
    }
    private void draw3(Canvas canvas) {
        fill3Bottom(canvas);
    }




    /**
     * 根据sin函数绘制波形
     *
     * @param canvas
     */
    private void drawSin(Canvas canvas) {
        fillSinBottom(canvas);
    }
    private void fill3Bottom(Canvas canvas) {
        φ -= waveSpeed / 200;
        float y;

        path3.reset();
        path3.moveTo(-50, 0);

        for (float x = 0; x <= getWidth(); x += 10) {
            y = (float) (A * Math.cos(ω * x + φ+Math.PI * startPeriod) +K);
//            y = (float) (A * Math.sin(ω * x + φ + Math.PI * startPeriod) + K);
            path3.lineTo(x, y);
        }

        //填充矩形
        path3.lineTo(getWidth(), getHeight());
        path3.lineTo(0, getHeight());
        path3.close();


        canvas.drawPath(path3, paint3);
    }

    private void fillCosBottom(Canvas canvas) {
        φ -= waveSpeed / 150;
        float y;

        path2.reset();
        path2.moveTo(0, 0);

        for (float x = 0; x <= getWidth(); x += 10) {
            y = (float) (A * Math.cos(ω * x + φ+Math.PI * startPeriod) +K);
//            y = (float) (A * Math.sin(ω * x + φ + Math.PI * startPeriod) + K);
            path2.lineTo(x, y);
        }

        //填充矩形
        path2.lineTo(getWidth(), getHeight());
        path2.lineTo(0, getHeight());
        path2.close();


        canvas.drawPath(path2, paint2);
    }

    /**
     * 填充波浪下面部分
     */
    private void fillSinBottom(Canvas canvas) {

        φ -= waveSpeed / 100;
        float y;

        path.reset();
        path.moveTo(0, 0);

        for (float x = 0; x <= getWidth(); x += 10) {
            y = (float) (A * Math.sin(ω * x + φ+ Math.PI * startPeriod)+K);
//            y = (float) (A * Math.sin(ω * x + φ + Math.PI * startPeriod) + K);
            path.lineTo(x, y);
        }

        //填充矩形
        path.lineTo(getWidth(), getHeight());
        path.lineTo(0, getHeight());
        path.close();


        canvas.drawPath(path, paint);

    }

    private void initAnimation() {
        valueAnimator = ValueAnimator.ofInt(0, getWidth());
        valueAnimator.setDuration(1000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                /**
                 * 刷新页面调取onDraw方法，通过变更φ 达到移动效果
                 */
                invalidate();
            }
        });
        if (waveStart) {
            valueAnimator.start();
        }
    }

    public void startAnimation() {
        if (valueAnimator != null) {
            valueAnimator.start();
        }
    }

    public void stopAnimation() {
        if (valueAnimator != null) {
            valueAnimator.cancel();
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void pauseAnimation() {
        if (valueAnimator != null) {
            valueAnimator.pause();
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void resumeAnimation() {
        if (valueAnimator != null) {
            valueAnimator.resume();
        }
    }

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getResources().getDisplayMetrics());
    }
}
